require "Common/define"
require "Logic/Config"
require "Common/functions"

EquipItem = {};
HelperFunc.SetUIEnv(EquipItem)
local this = EquipItem;

this.img_Icon = nil;
this.img_Quality = nil;
this.img_Frame = nil;
this.obj_Add = nil;
this.txt_Level = nil;
this.txt_Strength = nil;

function EquipItem.Init()
    local name,package = getUIName(PanelResNames.EquipItem);
    panelMgr:ShowUI(name,package, this.onCreate,Config.useLocalUI);
end

function EquipItem.onCreate(obj)
    obj:SetActive(false);
end
---@param obj FairyGUI.GComponent
function EquipItem.initData(obj, data, suitSpine)
    img_Icon = obj:GetChild("icon")
    img_Quality = obj:GetChild("quality")
    img_Frame = obj:GetChild("frame")
    obj_Add = obj:GetChild("add")
    txt_Level = obj:GetChild("level")
    txt_Strength = obj:GetChild("strength")
    --local ctrlRefineLevel = obj:GetController("refineLevel")
    local ctrlSuit = obj:GetController("suit")
    --local ctrlRefineSlot = obj:GetController("refineSlot")
    --local iconSuit = obj:GetChild("iconSuit")
    img_Icon.visible = false
    img_Quality.visible = false
    img_Frame.visible = false
    txt_Level.visible = false
    txt_Strength.visible = false
    obj_Add.visible = true
    ctrlSuit.selectedIndex = 0
    --iconSuit.url = ""
    --if ctrlRefineSlot then
    --    ctrlRefineSlot.selectedIndex = 0
    --end
    --ctrlRefineLevel.selectedIndex = 0

    if data ~= nil then
        if data["icon"] ~= nil then
            img_Icon.visible = true
            img_Icon.url = "ui://ItemAtlas/"..data["icon"]
        end
        if data["quality"] ~= nil then
            img_Quality.visible = true
            img_Frame.visible = true
            img_Frame.url =  EnumConst.AtlasPath.ICONAtlas..Config.IconFrame[tonumber(data["quality"])]
            img_Quality.url = EnumConst.AtlasPath.ICONAtlas..Config.IconQuality[tonumber(data["quality"])];
        end
        local cfg = dataTable.getDataTableByStringId("t_equip",data.itemModelId)
        ctrlSuit.selectedIndex = cfg.f_EquipSuit
        --iconSuit.url = EnumConst.AtlasPath.ICONAtlas..cfg.f_EquipSuitlcon

        if data["strength"] ~= nil and tonumber(data["strength"]) > 0 then
            txt_Strength.visible = true
            txt_Strength.text = "+"..data["strength"];
        end

            local refine = tonumber(data.curRfLevel)
            if refine ~= nil and refine > 0 then
                --if ctrlRefineLevel then
                --    ctrlRefineLevel.selectedIndex = refine
               -- end
                txt_Level.visible = true
                txt_Level.text = "+"..tostring(data["curRfLevel"])
            else
                --if ctrlRefineLevel then
                --    ctrlRefineLevel.selectedIndex = 0
                --end
                txt_Level.visible = false
            end

        obj_Add.visible = false
    end
    --if ctrlRefineSlot and data then
    --    ctrlRefineSlot.selectedIndex = logicMgr.EquipManager.GetMaxRefineLevel(data.itemModelId)
    --end
    if suitSpine then
        local crtSpine = obj:GetController("SuitColor")
        crtSpine.selectedIndex = suitSpine
    end
end
function EquipItem.HideRefineLevel(obj)
    local level = obj:GetController("refineLevel")
    level.selectedIndex = 0
end

return EquipItem;